<?php
require_once 'contato.class.php';
require_once 'control.class.php';
require_once 'mailer/class.phpmailer.php';

class ContatoModel extends Contato{
	public function insertContato($arr){
		try {
			$tot_f = count($arr['files']);
			if($tot_f > 0){
				for($i = 0; $i < $tot_f; $i++){		
					if($arr['files'][$i]['name'] != ''){
						$ext = pathinfo ($arr['files'][$i]['name']);
						$path = $arr['files'][$i]['tmp_name'];
						$nome = md5(microtime()).".".$ext['extension'];
						$new_path = '../imagens/anexos/'.$nome;
						if(!move_uploaded_file($path, $new_path)){
							throw new Exception ( "Ocorreu um erro ao salvar a imagem!" );
						}
					}
				}
			}
			$mail = new PHPMailer ();
			$mail->SetLanguage ( "br", "mailer/" );
			$mail->SMTP_PORT = "587";
			$mail->SMTPSecure = "tls";
			$mail->IsSMTP ();
			$mail->Host = "smtp.gmail.com";
			$mail->SMTPAuth = true;
			$mail->Username = 'clientes.circo@gmail.com';
			$mail->Password = 'freak25gomez';
			$mail->From 	= 'comercial@mtspromove.com.br';
			$mail->FromName = "Contato Site MTS";
			$mail->AddAddress ("diegoschefer@gmail.com","Programador");
			$mail->AddAddress ("comercial@mtspromove.com.br","Comercial MTS");
			$mail->WordWrap = 50;
			$mail->IsHTML (true);
			$mail->Subject = 'ATENCAO - CONTATO SITE MTS';
			foreach($arr as $chave => $valor){
				if($chave != 'files'){			
					if(!is_array($valor)){
						$txt .= $chave.':&nbsp;'.str_replace("'","",$valor).'<br/>';	
					}else{
						$txt .= $chave.':&nbsp;';
						foreach($valor as $sub){
							$txt .= str_replace("'","",$sub).' - '; 
						}
						$txt .= '<br/>';
					}
				}
			}
			//$txt .= $nome ? "Anexo desta mensagem: <a href=\"http://www.zondesign.com.br/imagens/anexos/$nome\">$nome</a>" : "";
			$mail->MsgHTML($txt);
			if(!$mail->Send()){
				throw new Exception("Ocorreu um erro ao enviar!");
			}
		} catch (Exception $e) {
			echo 'Erro: ',  $e->getMessage();
		}
	}
	public function pedidoContato($arr, $ticket){
		try {	
			session_start();
			$mail = new PHPMailer ();
			$mail->SetLanguage ( "br", "mailer/" );
			$mail->SMTP_PORT = "587";
			$mail->SMTPSecure = "tls";
			$mail->IsSMTP ();
			$mail->Host = "smtp.gmail.com";
			$mail->SMTPAuth = true;
			$mail->Username = 'clientes.circo@gmail.com';
			$mail->Password = 'freak25gomez';
			$mail->From 	= 'comercial@mtspromove.com.br';
			$mail->FromName = "MTS PROMOVE";
			$mail->AddAddress ("diegoschefer@gmail.com","Programador");
			$mail->AddAddress ("comercial@mtspromove.com.br","Comercial MTS");
			$mail->AddAddress ($_SESSION['setEmail']);
			$mail->WordWrap = 50;
			$mail->IsHTML (true);
			$mail->Subject = 'Novo Pedido MTS PROMOVE';

			$txt .= $ticket;
			$txt .= '<br />';
			$txt .= '<table width="100%">';
				$txt .= '<tr>';
					$txt .= '<th>Ref.</th>';
					$txt .= '<th>Quantidade</th>';
					$txt .= '<th>Vlr.</th>';
				$txt .= '</tr>';				
				$prod = explode(',',$arr['idprodutos']);
				$qntd = explode(',',$arr['quantidade']);
				foreach ($prod as $k => $v){
					$con = new Control('consultar','produtos',$v);
					$con->Consult();
					$item = $con->result->lista;
					$txt .=  '<tr align="center">';
						$txt .=  '<td>'.$item['referencia'].'</td>';
						$txt .=  '<td>'.$qntd[$k].'</td>';
						$txt .=  '<td>R$ '.number_format($item['valor_pacote'],2,',','.').'</td>';
					$txt .=  '</tr>';	
				}	
				
			$txt .= '</table>';
			$txt .= '<br /><br />';
			$txt .= '<h2>Valor total: R$ '.number_format($arr['valor'],2,',','.').'</h2>';
					
			$mail->MsgHTML($txt);
			if(!$mail->Send()){
				throw new Exception("Ocorreu um erro ao enviar!");
			}
		} catch (Exception $e) {
			echo 'Erro: ',  $e->getMessage();
		}
	}
}
?>